//左时间区间时间
var leftDate = 0;
// 左时间区间已读的计数FOR ESB总量
var leftValue4Total = 0;
// 左时间区间已读的计数FOR 请求成功
var leftValue4S = 0;
// 左时间区间已读的计数FOR 请求失败
var leftValue4F = 0;
var totalCharSeries;
var failCharSeries;
var successCharSeries;
$(function() {
	$(document).ready(function() {
		Highcharts.setOptions({
			global : {
				useUTC : false
			}
		});
		initCharts();
		loopRefresh();
	});
});

function initCharts() {
	$.ajax({
		type : "get",
		url : BASE_URL_API + '/quantityAnalysis/findQuantityAnalysisInfo?v='
				+ Math.random(),
		dataType : 'json',
		contentType : "application/json",
		success : function(data) {
			if (data.result == true) {
				// 将数据导入图表
				// alert(JSON.stringify(data));
				leftDate = data.leftDate;
				leftValue4Total = data.leftValue4Total;
				leftValue4S = data.leftValue4S;
				leftValue4F = data.leftValue4F;
				initialEsbAccTotalTimes(data.total);
				initialEsbResultReport(data.success, data.failures);
			} else {
				$.messager.alert('错误', '<p>' + data.msg + '</p>', 'error');
			}
		},
		error : function() {
			$.messager.alert('错误', '<p>服务异常，接口隔离失败，请稍后重试或联系管理员!</p>', 'error');
		}
	});
}
function loopRefresh() {
	update();
	setTimeout(loopRefresh, 5000);
}
function update() {
	if (totalCharSeries == null || failCharSeries == null
			|| successCharSeries == null) {
		return;
	}

	$.ajax({
		type : "get",
		url : BASE_URL_API + '/quantityAnalysis/getLatestQuantityInfo?v='
				+ Math.random(),
		dataType : 'json',
		data : {
			leftDate : leftDate,
			leftValue4Total : leftValue4Total,
			leftValue4S : leftValue4S,
			leftValue4F : leftValue4F
		},
		contentType : "application/json",
		success : function(data) {
			if (data.result == true) {
				// ESB总量数据 START=====================================
				leftDate = data.leftDate;
				leftValue4Total = data.leftValue4Total;
				var x = data.key;
				var y = data.valueTotal; // alert('x='+x+'
				// y='+y);
				totalCharSeries.addPoint([ x, y ], true, true);
				// ESB总量数据 END=====================================

				// 成功、失败数据 START=====================================
				leftValue4S = data.leftValue4S;
				leftValue4F = data.leftValue4F;// alert(leftValue4F+"
				// "+leftValue4S);
				var yS = data.valueS; // alert('x='+x+'
				// y='+y);
				var yF = data.valueF;
				successCharSeries.addPoint([ x, yS ], false, true);
				failCharSeries.addPoint([ x, yF ], true, true);
				// 成功、失败数据 END=====================================
			} else {
				// $.messager.alert('错误', '<p>' + data.msg +
				// '</p>', 'error');
			}
		},
		error : function() {
			// $.messager.alert('错误',
			// '<p>服务异常，接口隔离失败，请稍后重试或联系管理员!</p>', 'error');
		}
	});
}
function initialEsbAccTotalTimes(total) {
	$('#divEsbAccTotalTimes').highcharts(
			{
				chart : {
					type : 'spline',
					//animation : Highcharts.svg, // don't animate in old
					// IE
					marginRight : 10,
					events : {
						load : function() {
							totalCharSeries = this.series[0];
						}
					}
				},
				credits : {
					enabled : false
				},
				title : {
					text : '平台总流量'
				},
				 legend:{
					 margin: 0 
			        },
				xAxis : {
					type : 'datetime',
					tickPixelInterval : 30,
					title: {
						align: 'middle',
						style: {fontSize: '12px'},
			            text: "hh:mm:ss（时：分：秒）"	,
			            x: -20
					},
					labels : {
						step : 4,
						formatter : function() {
							return Highcharts.dateFormat('%H:%M:%S', this.value);
						}
					}
				},
				yAxis : {
					min : 0,
					labels: {
		                formatter: function() {
		                    return this.value ;
		                }
		            },
	            title: {
	                text: '单位(次)'
	            },
					plotLines : [ {
						value : 0,
						width : 1,
						color : '#808080'
					} ],
					labels : {
						formatter : function() {
							if (this.value == 0) {
								return this.value;
							}
							return this.value + '';
						}
					}
				},
				tooltip : {
					formatter : function() {
						return '<b>'
								+ this.series.name
								+ '</b><br/>'
								+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S',
										this.x) + '<br/>'
								+ Highcharts.numberFormat(this.y, 0);
					}
				},
				exporting : {
					enabled : false
				},
				series : [ {
					name : '平台总流量',
					data : (function() {
						// generate an array of random data
						var data = [];
						$.each(total, function(i, item) {
							data.push({
								x : item.key,
								y : item.value
							});
						});
						return data;
					})()
				} ]
			});

}
function initialEsbResultReport(success, failures) {
	Highcharts.setOptions({
		colors : [ '#24CBE5', '#ED561B' ]
	});
	$('#divEsbResultReport').highcharts(
			{
				chart : {
					type : 'spline',
					//animation : Highcharts.svg, // don't animate in old
					// IE
					marginRight : 10,
					events : {
						load : function() {
							successCharSeries = this.series[0];
							failCharSeries = this.series[1];
						}
					}
				},
				 legend:{	
					 	margin: 0
			        },
				title : {
					text : '成功/失败请求数'
				},
				credits : {
					enabled : false
				},
				xAxis : {
					type : 'datetime',
					tickPixelInterval : 30,
					labels : {
						step : 4,
						formatter : function() {
							return Highcharts.dateFormat('%H:%M:%S', this.value);
						}
					},
					title: {
						align: 'middle',
						style: {fontSize: '12px'},
			            text: "hh:mm:ss（时：分：秒）"	,
			            x: -20
					}
				},
				yAxis : {
					min : 0,
					labels: {
		                formatter: function() {
		                    return this.value;
		                }
		            },
	            title: {
	                text: '单位(次)'
	            },
					plotLines : [ {
						value : 0,
						width : 1,
						color : '#808080'
					} ]
				},
				tooltip : {
					formatter : function() {
						return '<b>'
								+ this.series.name
								+ '</b><br/>'
								+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S',
										this.x) + '<br/>'
								+ Highcharts.numberFormat(this.y, 0);
					}
				},
				exporting : {
					enabled : false
				},
				series : [ {
					name : '成功请求数',
					data : (function() {
						// generate an array of random data
						var data = [];
						$.each(success, function(i, item) {
							data.push({
								x : item.key,
								y : item.value
							});
						});
						return data;
					})()
				}, {
					name : '失败请求数',
					data : (function() {
						// generate an array of random data
						var data = [];
						$.each(failures, function(i, item) {
							lastValueF = item.value;
							data.push({
								x : item.key,
								y : item.value
							});
						});
						return data;
					})()
				} ]
			});
}
